'''
''' Click handler for Button that uploads a file to a Data subfolder which exists
''' in the current application directory.
'''
Public Overrides Sub ${Upload Button Control}_Click(ByVal sender As Object, ByVal args As EventArgs)
Dim inputFile As System.Web.UI.HtmlControls.HtmlInputFile
inputFile = CType(Me.Page.FindControlRecursively("inputFile"), System.Web.UI.HtmlControls.HtmlInputFile)
'' If this code customization was added to Table Control Row, replace FindControlRecursively() with FindControl()
'' or comment out the above line of code and uncomment the line below
'' inputFile = CType(Me.FindControl("inputFile"), System.Web.UI.HtmlControls.HtmlInputFile)
If (Not inputFile.PostedFile Is Nothing) And _
(inputFile.PostedFile.ContentLength > 0) Then
' Get the name of the file to be uploaded and
' the location where the file needs to be saved.
Dim fileName As String = System.IO.Path.GetFileName(inputFile.PostedFile.FileName)
Dim saveLocation As String = Me.Page.Server.MapPath("..\Data") & "\" & fileName
Try
'Save the file.
inputFile.PostedFile.SaveAs(saveLocation)
me.Page.Response.Write("The file has been uploaded.")
Catch exc As Exception
me.Page.Response.Write("Error: " & exc.Message)
End Try
Else
me.Page.Response.Write("Please select a file to upload.")
End If
End Sub
|